//
//  NumberDataSource.m
//  Tables
//
//  Created by mathias Muller on 8/21/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "NumberDataSource.h"


@implementation NumberDataSource
@synthesize selectedValues, length;
@synthesize textResult;


- (id)initWithLength:(NSInteger)l {
	if (self = [super init]) {
		NSMutableArray * selected = [[NSMutableArray alloc] init];
		for(int i=0;i<l;i++) {
			[selected addObject:@"0"];
		}
		self.selectedValues = selected;
		self.length = l;
	}
	return self;
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
	return self.length ; 
}

// returns the # of rows in each component..
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
	return 10;
}	

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
	return [[NSNumber numberWithInt:row] stringValue];
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
	return 60.0;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
	return 60.0;
}


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
	NSString * value =  [self pickerView:pickerView titleForRow:row forComponent:component];
	[self.selectedValues replaceObjectAtIndex:component withObject:value];
	[self updateText];
}

- (NSInteger) getSelectedValue {
	NSInteger result = 0;
	for(int i = 0;i<self.length;i++) {
		result += [[self.selectedValues objectAtIndex:i] intValue] * pow(10, self.length-1-i);
	}
	return result;
}

- (void) updateText {
	self.textResult.text = [NSString stringWithFormat:@"%d",[self getSelectedValue]];
}

- (void) reset {
	for(int i=0;i<self.length;i++) {
		[self.selectedValues replaceObjectAtIndex:i withObject:@"0"];
	}
	self.textResult.text = @"";
}
@end
